//
//  LQSwitchView.m
//  LQSwitchView
//
//  Created by ianyumi on 2017/6/2.
//  Copyright © 2017年 ianyumi. All rights reserved.
//

#import "LQSwitchView.h"

@interface LQSwitchView ()

@property (nonatomic, strong) NSArray <NSString *> *titles;
@property (nonatomic, strong) NSMutableArray *titleBtns;

/** 背景 */
@property (nonatomic, weak) UIView *bgView;
/** 光标 */
@property (nonatomic, weak) UIView *cursorView;

@end

@implementation LQSwitchView {
    UIButton *_curSelectedBtn;
}

- (instancetype)initWithTitles:(NSArray<NSString *> *)titles {
    if (self = [super init]) {
        _titles = titles;
        [self setup];
    }
    return self;
}

- (void)setup {
    self.backgroundColor = [UIColor clearColor];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.cursorView.backgroundColor = [UIColor redColor];
    [self.titles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn addTarget:self action:@selector(changeSelect:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        [self.titleBtns addObject:btn];
        if (idx == 0) {
            _curSelectedBtn = btn;
            _curSelectedBtn.enabled = NO;
        }
    }];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.layer.cornerRadius = self.bounds.size.height * 0.5;
    self.layer.masksToBounds = YES;
    
    self.bgView.frame = self.bounds;
    CGFloat margin = 6;
    __block CGFloat x = 0;
    __block CGFloat y = margin;
    CGFloat w = (self.bounds.size.width - (self.titles.count + 1) * margin) / self.titles.count;
    CGFloat h = self.bounds.size.height - margin * 2;
    [self.titleBtns enumerateObjectsUsingBlock:^(UIButton *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        x = margin + idx * w + margin * idx;
        obj.frame = CGRectMake(x, y, w, h);
    }];
    UIButton *cur = _curSelectedBtn;
    self.cursorView.bounds = CGRectMake(0, 0, cur.bounds.size.width + 2, cur.bounds.size.height + 2);
    self.cursorView.center = cur.center;
    self.cursorView.layer.cornerRadius = self.cursorView.bounds.size.height * 0.5;
    self.cursorView.layer.masksToBounds = YES;
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    if (selectedIndex < self.titles.count) {
        [self changeSelect:self.titleBtns[selectedIndex]];
    }
}

#pragma mark - 懒加载
- (UIView *)bgView {
    if (_bgView == nil) {
        UIView *bv = [UIView new];
        [self addSubview:_bgView = bv];
    }
    return _bgView;
}
- (UIView *)cursorView {
    if (_cursorView == nil) {
        UIView *cv = [UIView new];
        [self insertSubview:cv aboveSubview:self.bgView];
        _cursorView = cv;
    }
    return _cursorView;
}
- (NSMutableArray *)titleBtns {
    if (_titleBtns == nil) {
        _titleBtns = [NSMutableArray array];
    }
    return _titleBtns;
}

#pragma mark - 按钮事件
- (void)changeSelect:(UIButton *)btn {
    _curSelectedBtn.enabled = YES;
    btn.enabled = NO;
    _curSelectedBtn = btn;
    [self setNeedsLayout];
}

@end
